<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="close">
   <el-form ref="deptForm" :model="deptForm" :rules="rules" label-width="120px">
  <el-form-item label="部门名称" prop="name">
    <el-input v-model="deptForm.name" size="mini" style="width: 80%" />
  </el-form-item>
  <el-form-item label="部门类型" prop="type">
    <el-select v-model="deptForm.type" size="mini">
      <el-option label="分公司" value="分公司" />
      <el-option label="部门" value="部门" />
    </el-select>
  </el-form-item>
  <el-form-item label="部门负责人" prop="managerName">
    <el-select v-model="deptForm.managerName" size="mini">
      <el-option label="张三三" value="张三三" />
      <el-option label="李四四" value="李四四" />
      <el-option label="何笑笑" value="何笑笑" />
      <el-option label="李维" value="李维" />
    </el-select>
  </el-form-item>
  <el-form-item label="部门描述" prop="introduce">
    <el-input v-model="deptForm.introduce" type="textarea" style="width: 80%" :rows="2" />
  </el-form-item>
  <el-form-item>
    <el-row type="flex" justify="center">
      <el-col :span="16">
        <el-button size="mini" type="primary" @click="submit">确定</el-button>
        <el-button size="mini" @click="close">取消</el-button>
      </el-col>
    </el-row>
  </el-form-item>
</el-form>
  </el-dialog>
</template>

<script>
import dayjs from 'dayjs'
import { addDepartmentList, getDepartmentDetail, updateDepartment } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentPid: {
      type: Number
    }
  },
  data () {
    return {
      deptForm: {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 8, message: '部门名称是1-8位字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '部门类型不能为空', trigger: 'blur' }
        ],
        managerName: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取部门详情事件
    async  getDepartmentDetail () {
      const res = await getDepartmentDetail(this.currentPid)
      this.deptForm = res.data
    },
    // 确定点击事件
    async  submit () {
      if (this.deptForm.id) {
        await updateDepartment(this.deptForm)
        this.$message.success('编辑部门成功')
      } else {
        this.deptForm.pid = this.currentPid
        this.deptForm.createTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
        await addDepartmentList(this.deptForm)
        this.$message.success('新增部门成功')
      }
      this.close()
      this.$emit('updateDep')
    },
    // 监听弹层关闭
    close () {
      this.deptForm = {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
